<?php

class Meals extends CI_Model{
    function __construct(){
        parent::__construct();
    }
    
    function getAll(){
        $this->db->order_by('meal_title','asc');
        return $this->db->get('meals');
    }
    
    function getByTitle($title){
        $this->db->from('meals AS M');
        $this->db->where('meal_title', $title);
        $meal = $this->db->get();
        if ($meal->num_rows() > 0){
            return $meal->row();
        }
        else{
            return null;
        }
    }
    
    function getWhere($title = "", $category = ""){
        $this->db->from('meal_categories AS MC');
        $this->db->join('meals AS M', 'M.meal_id = MC.meal_id');
        
        if (trim($title) != ""){
            $this->db->like("M.meal_title",$title);
        }
        
        if (trim($category) != -1){
            $this->db->where("MC.category_id",$category);
        }
        $this->db->order_by('M.meal_title', "asc");
        return $this->db->get();
    }
    
    function getMealInfo($id){
        $this->db->where('meal_id', $id);
        
        $meal = $this->db->get("meals");
        if ($meal->num_rows() > 0){
            return $meal->row();
        }
        else{
            return null;
        }
    }
    
    function getIngredients($id){
        $this->db->from('meal_ingredients AS MI');
        $this->db->join('ingredients AS I', 'I.ingredient_id = MI.ingredient_id');
        
        $this->db->where("MI.meal_id",$id);
        
        return $this->db->get();
    }
    
    function getCategories($id){
        $this->db->from('meal_categories AS MC');
        $this->db->join('categories AS C', 'C.category_id = MC.category_id');
        
        $this->db->where("MC.meal_id",$id);
        
        return $this->db->get();
    }
    
    function create($meal_title,$categories,$ingredients,$description){
        
        $this->db->trans_start();
        
        $data["meal_title"] = $meal_title;
        $data["meal_description"] = $description;
        $query = $this->db->insert('meals', $data);
        $meal_id = $this->db->insert_id();

        unset($data);
        $data['meal_id'] = $meal_id;

        foreach($ingredients as $ingredient){
            $data['ingredient_id'] = $ingredient;
            $query = $query && $this->db->insert('meal_ingredients',$data);
        }

        unset($data);
        $data['meal_id'] = $meal_id;

        foreach($categories as $category){
            $data['category_id'] = $category;
            $query = $query && $this->db->insert('meal_categories',$data);
        }
        
        $this->db->trans_complete();
        
        if ($query){
            return true;
        }
        else{
            return false;
        }
        
    }
    
    function update($meal_id, $meal_title,$categories,$ingredients,$description){
        
        $this->db->trans_start();
        
        //Remove all ingredients and categories
        $this->db->where('meal_id', $meal_id);
        $this->db->delete('meal_categories');
        
        $this->db->where('meal_id', $meal_id);
        $this->db->delete('meal_ingredients'); 
        
        $data["meal_title"] = $meal_title;
        $data["meal_description"] = $description;
        $this->db->where('meal_id',$meal_id);
        $query = $this->db->update('meals', $data);

        unset($data);
        $data['meal_id'] = $meal_id;

        foreach($ingredients as $ingredient){
            $data['ingredient_id'] = $ingredient;
            $query = $query && $this->db->insert('meal_ingredients',$data);
        }

        unset($data);
        $data['meal_id'] = $meal_id;

        foreach($categories as $category){
            $data['category_id'] = $category;
            $query = $query && $this->db->insert('meal_categories',$data);
        }
        
        $this->db->trans_complete();
        
        if ($query){
            return true;
        }
        else{
            return false;
        }
    }
    
    function deleteMeal($meal_id){
        $this->db->trans_start();
        
        $this->db->where('meal_id',$meal_id);
        $this->db->delete('meal_categories');
                
        $this->db->where('meal_id',$meal_id);
        $this->db->delete('meal_ingredients');
        
        $this->db->where('meal_id',$meal_id);
        $this->db->delete('mealtime_meals');
        
        $this->db->where('meal_id',$meal_id);
        $this->db->delete('meals');
        
        $this->db->trans_complete();
        return $this->db->trans_status();
    }
    
    function getBasetypes($meal_id){
        $this->db->from('meal_ingredients as MI');
        $this->db->join('basetype_ingredients AS BI', 'BI.ingredient_id = MI.ingredient_id');
        $this->db->where('MI.meal_id',$meal_id);
        $this->db->group_by('BI.basetype_id');
        $this->db->select('BI.basetype_id');
        
        $query = $this->db->get();
        
        if ($query->num_rows() > 0){
            return $query;
        }
        else{
            return null;
        }
    }
    
    function isToday($meal_id){
        
        if ($meal_id < 0){
            return false;
        }
        
        $this->db->where('mealtime_date',  date_to_mysql(date('Y'),date('n'), date('j')));
        $query = $this->db->get("mealtimes");
        
        if ($query->num_rows() > 0){
            if ($query->row()->mealtime_orderable == 1){
                $this->db->where('mealtime_id', $query->row()->mealtime_id);
                $this->db->where('meal_id', $meal_id);
                $query = $this->db->get("mealtime_meals");

                if ($query->num_rows() > 0){
                    return true;
                }
            }
        }
        
        return false;
    }
}

